{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Valid Mountain Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: validMountainArray"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #有效的山脉数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个整数数组 <code>arr</code>，如果它是有效的山脉数组就返回&nbsp;<code>true</code>，否则返回 <code>false</code>。</p>\n",
    "\n",
    "<p>让我们回顾一下，如果 <code>arr</code>&nbsp;满足下述条件，那么它是一个山脉数组：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>arr.length &gt;= 3</code></li>\n",
    "\t<li>在&nbsp;<code>0 &lt; i&nbsp;&lt; arr.length - 1</code>&nbsp;条件下，存在&nbsp;<code>i</code>&nbsp;使得：\n",
    "\t<ul>\n",
    "\t\t<li><code>arr[0] &lt; arr[1] &lt; ... arr[i-1] &lt; arr[i] </code></li>\n",
    "\t\t<li><code>arr[i] &gt; arr[i+1] &gt; ... &gt; arr[arr.length - 1]</code></li>\n",
    "\t</ul>\n",
    "\t</li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2019/10/20/hint_valid_mountain_array.png\" style=\"height: 316px; width: 500px;\" /></p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>arr = [2,1]\n",
    "<strong>输出：</strong>false\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>arr = [3,5,5]\n",
    "<strong>输出：</strong>false\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>arr = [0,3,2,1]\n",
    "<strong>输出：</strong>true</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= arr.length &lt;= 10<sup>4</sup></code></li>\n",
    "\t<li><code>0 &lt;= arr[i] &lt;= 10<sup>4</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [valid-mountain-array](https://leetcode.cn/problems/valid-mountain-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [valid-mountain-array](https://leetcode.cn/problems/valid-mountain-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[2,1]', '[3,5,5]', '[0,3,2,1]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        if len(arr) < 3:return False\n",
    "        maxIdx = arr.index(max(arr))\n",
    "        if maxIdx == len(arr) - 1 or maxIdx == 0:return False\n",
    "        for i in range(1,maxIdx + 1):\n",
    "            if arr[i] <= arr[i-1]:\n",
    "                return False\n",
    "        for i in range(maxIdx,len(arr)-1):\n",
    "            if arr[i] <= arr[i+1]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        left, right = 0, len(arr) - 1\n",
    "        while left < len(arr) - 1 and arr[left + 1] > arr[left]:\n",
    "            left += 1\n",
    "        while right > 0 and arr[right - 1] > arr[right]:\n",
    "            right -= 1\n",
    "        return left == right and left != len(arr) - 1 and right != 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        peak = False\n",
    "        left = 0\n",
    "        right = 0\n",
    "        for i in range(1,len(arr)):\n",
    "            if arr[i] > arr[i-1] and right == 0:\n",
    "                left += 1\n",
    "            elif arr[i] < arr[i-1] and left > 0:\n",
    "                right += 1\n",
    "            else:\n",
    "                return False\n",
    "        return left > 0 and right > 0\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        if len(arr) < 3:\n",
    "            return False\n",
    "        idx = arr.index(max(arr))\n",
    "        a = arr[:idx+1]\n",
    "        b = arr[idx:]\n",
    "        if len(a)<2 or len(b)<2:\n",
    "            return False\n",
    "        a_ = sorted(a)\n",
    "        b_ = sorted(b)\n",
    "        return a == sorted(list(set(a_))) and b[::-1] == sorted(list(set(b_)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        maxa = arr.index(max(arr))\n",
    "        # print(maxa)\n",
    "        a = False\n",
    "        if 0 < maxa < len(arr)-1:\n",
    "            left,right = arr[:maxa+1],arr[maxa:]\n",
    "            # print(left,right)\n",
    "            if all(x<y for x, y in zip(left, left[1:])) and all(x>y for x, y in zip(right, right[1:])):\n",
    "                a = True\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        n = len(arr)\n",
    "        if n <= 2: return False\n",
    "       \n",
    "        left, right = 0, n - 1\n",
    "\n",
    "      \n",
    "        while left + 1 < right and arr[left] < arr[left + 1]:\n",
    "            left += 1\n",
    "        \n",
    "        while right - 1 >= 0 and arr[right - 1] > arr[right]:\n",
    "            right -= 1\n",
    "        \n",
    "        if left != 0 and right != n -1:\n",
    "            return left == right\n",
    "        \n",
    "        return False\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        N = len(arr)\n",
    "        i = 0\n",
    "\n",
    "        # 递增扫描\n",
    "        while i + 1 < N and arr[i] < arr[i + 1]:\n",
    "            i += 1\n",
    "\n",
    "        # 最高点不能是数组的第一个位置或最后一个位置\n",
    "        if i == 0 or i == N - 1:\n",
    "            return False\n",
    "\n",
    "        # 递减扫描\n",
    "        while i + 1 < N and arr[i] > arr[i + 1]:\n",
    "            i += 1\n",
    "\n",
    "        return i == N - 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        len_arr = len(arr)\n",
    "        left = 0\n",
    "        right = len_arr - 1\n",
    "        #从左边往右边找，一直找到山峰为止\n",
    "        while (left + 1 < len_arr and arr[left] < arr[left + 1]):\n",
    "            left+=1\n",
    "        #从右边往左边找，一直找到山峰为止\n",
    "        while (right > 0 and arr[right - 1] > arr[right]):\n",
    "            right-=1\n",
    "        #判断从左边和从右边找的山峰是不是同一个\n",
    "        return left > 0 and right < len_arr - 1 and left == right"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        n = len(arr)\n",
    "        if n < 3:   return False\n",
    "\n",
    "        mid = arr.index(max(arr))\n",
    "        if mid == 0 or mid == n - 1:\n",
    "            return False\n",
    "\n",
    "        for i in range(1, mid):\n",
    "            if arr[i] <= arr[i - 1]:\n",
    "                return False\n",
    "        \n",
    "        for i in range(mid + 1, n):\n",
    "            if arr[i] >= arr[i - 1]:\n",
    "                return False\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        # 双指针，一个从左边找最高山峰，一个从右边找最高山峰，最后判断找到的是不是同一个山峰\n",
    "        n = len(arr)\n",
    "        l,r = 0,n-1 # 定义两个指针的位置\n",
    "        # 从左边往右边找，一直找到山峰为止\n",
    "        while l < n - 1 and arr[l+1] > arr[l]:\n",
    "            l += 1\n",
    "        # 从右边往左边找，一直找到山峰为止\n",
    "        while r > 0 and arr[r] < arr[r-1]:\n",
    "            r -= 1\n",
    "        # 判断从左边和从右边找的山峰是不是同一个\n",
    "        return arr[l] == arr[r] and r == l and l < n-1 and r > 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        i=1\n",
    "        if len(arr)==1:\n",
    "            return False\n",
    "        if arr[0]>=arr[1]:\n",
    "            return False\n",
    "        while(i<len(arr)-1):\n",
    "            if arr[i]==arr[i-1]:\n",
    "                return False\n",
    "            elif arr[i]<arr[i-1]:\n",
    "                i=i+1\n",
    "                break\n",
    "            else:\n",
    "                i=i+1\n",
    "        \n",
    "        while(i<len(arr)):\n",
    "            if arr[i]>=arr[i-1]:\n",
    "                return False\n",
    "            i+=1\n",
    "        return True        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        len_arr = len(arr)\n",
    "        left = 0\n",
    "        right = len_arr - 1\n",
    "        #从左边往右边找，一直找到山峰为止\n",
    "        while (left + 1 < len_arr and arr[left] < arr[left + 1]):\n",
    "            left+=1\n",
    "        #从右边往左边找，一直找到山峰为止\n",
    "        while (right > 0 and arr[right - 1] > arr[right]):\n",
    "            right-=1\n",
    "        #判断从左边和从右边找的山峰是不是同一个\n",
    "        return left > 0 and right < len_arr - 1 and left == right"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        N = len(arr)\n",
    "        i = 0\n",
    "        while i+1 < N and arr[i] < arr[i+1]:\n",
    "            i += 1\n",
    "        \n",
    "        if i == 0 or i == N -1:\n",
    "            return False\n",
    "        while i + 1 < N and arr[i] > arr[i+1]:\n",
    "            i += 1\n",
    "        return i == N - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        m=arr.index(max(arr))\n",
    "        i=0\n",
    "        n=len(arr)\n",
    "        if n<3 or m==0 or m==n-1:\n",
    "            return False\n",
    "        while(i<m):\n",
    "            if(arr[i]>=arr[i+1]):\n",
    "                return False\n",
    "            i=i+1\n",
    "        while(i<n-1):\n",
    "            if(arr[i]<=arr[i+1]):\n",
    "                return False\n",
    "            i=i+1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        n = len(arr)\n",
    "        l = 0 \n",
    "        count = 0\n",
    "        while l<n-1 and arr[l] < arr[l+1]:\n",
    "            count += 1\n",
    "            l+=1\n",
    "        if count < 1 or l==n-1:\n",
    "            return False\n",
    "        while l<n-1 and arr[l] > arr[l+1]:\n",
    "            l+=1\n",
    "        if l!=n-1:\n",
    "            return False\n",
    "        return True\n",
    "        \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        if len(arr)<3:\n",
    "            return False\n",
    "        left,right=0,len(arr)-1\n",
    "        while left<len(arr)-1 and arr[left]<arr[left+1]:\n",
    "            left+=1\n",
    "        while right>=1 and arr[right-1]>arr[right]:\n",
    "            right-=1\n",
    "        return left==right and left!=len(arr)-1 and right!=0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        n = len(arr)\n",
    "        if n < 3:\n",
    "            return False\n",
    "        i = 0\n",
    "        while i < n-1 and arr[i] < arr[i+1]:\n",
    "            i += 1\n",
    "        \n",
    "        if i == 0 or i == n-1:\n",
    "            return False\n",
    "        \n",
    "        while i < n-1 and arr[i] > arr[i+1]:\n",
    "            i += 1\n",
    "        \n",
    "        return i == n-1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        n=len(arr)\n",
    "        if n<3:return False\n",
    "        #i=0,j=n-1\n",
    "        i,j=0,n-1\n",
    "        while i<n-2 and arr[i]<arr[i+1]:\n",
    "            i+=1\n",
    "        while j>1 and arr[j]<arr[j-1]:\n",
    "            j-=1\n",
    "        return j==i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        n = len(arr)\n",
    "        if n < 3:\n",
    "            return False\n",
    "\n",
    "        i = 0\n",
    "        while i < n-1 and arr[i+1] > arr[i]:\n",
    "            i += 1\n",
    "\n",
    "        if i == 0 or i == n-1:\n",
    "            return False\n",
    "\n",
    "        while i < n-1 and arr[i+1] < arr[i]:\n",
    "            i += 1\n",
    "\n",
    "        return i == n-1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        if len(arr) < 3:\n",
    "            return False\n",
    "        i, j = 0, len(arr)-1\n",
    "        while i < j:\n",
    "            if arr[i]<arr[i+1]:\n",
    "                i += 1\n",
    "            elif arr[j]<arr[j-1]:\n",
    "                j -= 1\n",
    "            else:\n",
    "                break\n",
    "        if i==j and i!=0 and i != len(arr)-1:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        a = max(arr)\n",
    "        if len(arr)<3 or a == arr[-1] or a == arr[0]:\n",
    "            return False\n",
    "        \n",
    "        i = 1\n",
    "        j = len(arr)-1\n",
    "        while i<j:\n",
    "            while arr[i-1]<arr[i] and i<j:\n",
    "                i = i+1\n",
    "                if arr[i-1]>=arr[i]:\n",
    "                    break\n",
    "            while arr[j]<arr[j-1] and i<j:\n",
    "                j = j-1\n",
    "                if arr[j]>=arr[j-1]:\n",
    "                    break\n",
    "            if arr[i-1]>=arr[i] and arr[j]>=arr[j-1]:\n",
    "                break\n",
    "        if i == j:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        if len(arr) < 3:\n",
    "            return False\n",
    "        for i in range(1, len(arr) + 1):\n",
    "            if i == len(arr):\n",
    "                break\n",
    "            if arr[i] == arr[i-1]:\n",
    "                return False\n",
    "            if arr[i] < arr[i-1]:\n",
    "                break\n",
    "        if i == len(arr)or i == 1:\n",
    "            return False\n",
    "        for j in range(i, len(arr)-1):\n",
    "            if arr[j] <= arr[j+1]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        n=len(arr)\n",
    "        left=0\n",
    "        right=n-1\n",
    "        while left+1<n and arr[left]<arr[left+1]:\n",
    "            left+=1\n",
    "        while right - 1 > 0 and arr[right - 1] > arr[right]:\n",
    "            right -= 1\n",
    "        if left > 0 and right < n - 1 and left == right:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        if len(arr) < 3:\n",
    "            return False\n",
    "        n = arr.index(max(arr))\n",
    "        if n == 0 or n == len(arr) - 1:\n",
    "            return False\n",
    "        for i in range(n):\n",
    "            if arr[i + 1] <= arr[i]:\n",
    "                return False\n",
    "        for i in range(n, len(arr) - 1):\n",
    "            if arr[i + 1] >= arr[i]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        n = len(arr)\n",
    "        i = 0\n",
    "        while i+1<n and arr[i] < arr[i+1]:\n",
    "            i+=1\n",
    "        if i == 0 or i == n-1:\n",
    "            return False\n",
    "        while i+1<n and arr[i] > arr[i+1]:\n",
    "            i+=1\n",
    "        return i == n-1\n",
    "\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        left, right = 0, len(arr) - 1\n",
    "        for i in range(1, len(arr)):\n",
    "            if arr[i] > arr[i - 1]:\n",
    "                left += 1\n",
    "            else:\n",
    "                break\n",
    "        for i in range(len(arr) - 1, 0, -1):\n",
    "            if arr[i] < arr[i - 1]:\n",
    "                right -= 1\n",
    "            else:\n",
    "                break\n",
    "\n",
    "        if left == right and left != len(arr) - 1 and right != 0:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        n = len(arr)\n",
    "        if n < 3: return False\n",
    "        i, j = 0, n-1\n",
    "        \n",
    "        while i < n-2 and arr[i+1] > arr[i]: \n",
    "        \ti += 1    \n",
    "        while j > 1 and arr[j] < arr[j-1]:  \n",
    "        \tj -= 1\n",
    "        \n",
    "        return i == j           \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        a = max(arr)\n",
    "        if len(arr)<3 or a == arr[-1] or a == arr[0]:\n",
    "            return False\n",
    "        \n",
    "        i = 1\n",
    "        j = len(arr)-1\n",
    "        while i<j:\n",
    "            while arr[i-1]<arr[i] and i<j:\n",
    "                i = i+1\n",
    "                if arr[i-1]>=arr[i]:\n",
    "                    break\n",
    "            while arr[j]<arr[j-1] and i<j:\n",
    "                j = j-1\n",
    "                if arr[j]>=arr[j-1]:\n",
    "                    break\n",
    "            if arr[i-1]>=arr[i] and arr[j]>=arr[j-1]:\n",
    "                break\n",
    "        if i == j:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        left, right = 0, len(arr) - 1\n",
    "        while left + 1 <= len(arr) - 1 and arr[left + 1] > arr[left]:\n",
    "            left += 1\n",
    "        while right - 1 >= 0 and arr[right - 1] > arr[right]:\n",
    "            right -= 1\n",
    "        if left == right:\n",
    "            if left != 0 and left != len(arr) - 1:\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "       \n",
    "        f, n = 1, len(arr)\n",
    "        max1 = max(arr)\n",
    "        \n",
    "        if n < 3 or arr[-1] == max1 or arr[0] == max1:\n",
    "            return False      \n",
    "        \n",
    "        for i in range(n - 1):\n",
    "            if arr[i + 1] < max1 and f:\n",
    "                if arr[i + 1] <= arr[i]:\n",
    "                    return False\n",
    "            elif arr[i + 1] == max1:\n",
    "                f = 0\n",
    "            else:\n",
    "                if arr[i + 1] >= arr[i]:\n",
    "                    return False\n",
    "                    \n",
    "        return True\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        n = len(arr)\n",
    "        idx = arr.index(max(arr))\n",
    "        if idx == 0 or idx == n - 1:\n",
    "            return False\n",
    "        for i in range(idx):\n",
    "            if arr[i] >= arr[i + 1]:\n",
    "                return False\n",
    "        for  i in range(idx, n - 1):\n",
    "            if arr[i] <= arr[i + 1]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        # # uf, df = False, False\n",
    "        # count = 0\n",
    "        # for i in range(1, len(arr) - 1):\n",
    "        #     if arr[i - 1] < arr[i] and arr[i] > arr[i + 1]:\n",
    "        #         count += 1\n",
    "        #         if count > 1:\n",
    "        #             return False\n",
    "        #\n",
    "        # return count == 1\n",
    "        if len(arr) == 1:\n",
    "            return False\n",
    "\n",
    "        uf, df = False, False\n",
    "        for i in range(len(arr) - 1):\n",
    "            if arr[i] < arr[i + 1]:\n",
    "                if not uf:\n",
    "                    uf = True\n",
    "            elif arr[i] == arr[i + 1]:\n",
    "                return False\n",
    "            else:\n",
    "                if not uf:\n",
    "                    return False\n",
    "                break\n",
    "        for j in range(i, len(arr) - 1):\n",
    "            if arr[j] <= arr[j + 1]:\n",
    "                return False\n",
    "            elif not df:\n",
    "                df = True\n",
    "\n",
    "        return uf and df\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        if len(arr) < 3:\n",
    "            return False\n",
    "        if arr[1] > arr[0]:\n",
    "            up = True\n",
    "        else:\n",
    "            return False\n",
    "        for i in range(len(arr) - 1):\n",
    "            if up == True:\n",
    "                if arr[i + 1] > arr[i]:\n",
    "                    continue\n",
    "                elif arr[i + 1] == arr[i]:\n",
    "                    return False\n",
    "                else:\n",
    "                    up = False\n",
    "                    continue\n",
    "            else:\n",
    "                if arr[i + 1] < arr[i]:\n",
    "                    continue\n",
    "                else:\n",
    "                    return False\n",
    "        if up == True:\n",
    "            return False \n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        n=len(arr)\n",
    "        if n <3:\n",
    "            return False\n",
    "        else:\n",
    "            a=arr.index(max(arr))\n",
    "            if a==n-1 or a==0:\n",
    "                return False\n",
    "\n",
    "            for i in range(a):\n",
    "                if arr[i]>=arr[i+1]:\n",
    "                 return False\n",
    "            for i in range(a,n-1):\n",
    "                if arr[i]<=arr[i+1]:\n",
    "                 return False\n",
    "            return True  \n",
    "                  \n",
    "                 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        if len(arr) < 3:\n",
    "            return False\n",
    "        max_sofar = max(arr)\n",
    "        i = 0\n",
    "        while i < len(arr) and arr[i] < max_sofar:\n",
    "            if arr[i + 1] <= arr[i]:\n",
    "                return False\n",
    "            i += 1\n",
    "        if i == len(arr) - 1 or i == 0:\n",
    "            return False\n",
    "\n",
    "        while i < len(arr) - 1:\n",
    "            if arr[i + 1] >= arr[i]:\n",
    "                return False\n",
    "            i += 1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        n = len(arr)\n",
    "        if n<3:\n",
    "            return False\n",
    "        i = 1\n",
    "        while i < n and arr[i]>arr[i-1]:\n",
    "            i+=1\n",
    "        if i==1 or i==n:\n",
    "            return False\n",
    "        while i<n and arr[i]<arr[i-1]:\n",
    "            i+=1\n",
    "        return i==n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        N = len(arr)\n",
    "        i = 0\n",
    "\n",
    "        # 递增扫描\n",
    "        while i + 1 < N and arr[i] < arr[i + 1]:\n",
    "            i += 1\n",
    "\n",
    "        # 最高点不能是数组的第一个位置或最后一个位置\n",
    "        if i == 0 or i == N - 1:\n",
    "            return False\n",
    "\n",
    "        # 递减扫描\n",
    "        while i + 1 < N and arr[i] > arr[i + 1]:\n",
    "            i += 1\n",
    "\n",
    "        return i == N - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        # # uf, df = False, False\n",
    "        # count = 0\n",
    "        # for i in range(1, len(arr) - 1):\n",
    "        #     if arr[i - 1] < arr[i] and arr[i] > arr[i + 1]:\n",
    "        #         count += 1\n",
    "        #         if count > 1:\n",
    "        #             return False\n",
    "        #\n",
    "        # return count == 1\n",
    "        if len(arr) == 1:\n",
    "            return False\n",
    "\n",
    "        uf, df = False, False\n",
    "        for i in range(len(arr) - 1):\n",
    "            if arr[i] < arr[i + 1]:\n",
    "                uf = True\n",
    "            elif arr[i] == arr[i + 1]:\n",
    "                return False\n",
    "            else:\n",
    "                if not uf:\n",
    "                    return False\n",
    "                break\n",
    "        for j in range(i, len(arr) - 1):\n",
    "            if arr[j] <= arr[j + 1]:\n",
    "                return False\n",
    "            elif not df:\n",
    "                df = True\n",
    "\n",
    "        return uf and df\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        if len(arr)<3:\n",
    "            return False\n",
    "        flag=0\n",
    "        if arr[1]<arr[0]:\n",
    "            return False\n",
    "        for i in range(1,len(arr)):\n",
    "            if arr[i]==arr[i-1]:\n",
    "                return False\n",
    "            if arr[i]<arr[i-1]:\n",
    "                flag=1\n",
    "            if arr[i]>arr[i-1] and flag==1:\n",
    "                return False\n",
    "        return flag==1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        if len(arr)<3:\n",
    "            return False\n",
    "        cur=0\n",
    "        while cur<len(arr)-1:\n",
    "            cur+=1\n",
    "            if arr[cur]==arr[cur-1]:\n",
    "                return False\n",
    "            if arr[cur]<arr[cur-1]:\n",
    "                cur-=1\n",
    "                break\n",
    "        if cur==0 or cur==len(arr)-1:\n",
    "            return False\n",
    "        while cur<len(arr)-1:\n",
    "            cur+=1\n",
    "            if arr[cur]>=arr[cur-1]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        n = len(arr)\n",
    "        if n < 3:\n",
    "            return False\n",
    "        left, right = 0, n - 1\n",
    "        while left < n-1 and arr[left] < arr[left + 1]:\n",
    "            left += 1\n",
    "        while right > 0 and arr[right] < arr[right - 1]:\n",
    "            right -= 1\n",
    "        if left == right and left != 0 and right != n - 1:\n",
    "            return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        flag = 0\n",
    "        flag_l = flag_r = 0\n",
    "        for i in range(1, len(arr)):\n",
    "            if flag == 0:\n",
    "                if arr[i] > arr[i-1]: \n",
    "                    flag_l = 1\n",
    "                elif arr[i] < arr[i-1]: \n",
    "                    flag = 1\n",
    "                else: return False\n",
    "            if flag == 1:\n",
    "                if arr[i] < arr[i-1]: \n",
    "                    flag_r = 1\n",
    "                elif arr[i] > arr[i-1]: \n",
    "                    flag = 2\n",
    "                else: return False\n",
    "            if flag == 2: return False\n",
    "        return bool(flag_l and flag_r)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        if len(arr) < 3 or arr[0] >= arr[1]:\n",
    "            return False\n",
    "        flag = 1\n",
    "        for i in range(1, len(arr)):\n",
    "            if arr[i] > arr[i - 1] and flag == 1:\n",
    "                flag = 1\n",
    "            elif arr[i] > arr[i - 1] and flag == 0 or arr[i] == arr[i - 1] :\n",
    "                return False\n",
    "            elif arr[i] < arr[i - 1] and flag == 1:\n",
    "                flag = 0\n",
    "        return True if flag == 0 else False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        n = len(arr)\n",
    "        if n < 3:\n",
    "            return False\n",
    "        \n",
    "        i = 0\n",
    "        while i < n - 1 and arr[i] < arr[i + 1]: i += 1\n",
    "        if i == 0 or i == n - 1: return False\n",
    "        while i < n - 1 and arr[i] > arr[i + 1]:i += 1\n",
    "        return i == n - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        if len(arr)<3:\n",
    "            return False\n",
    "        flag = None\n",
    "        for i in range(1,len(arr)):\n",
    "            if arr[i] - arr[i-1] > 0 and flag is None:\n",
    "                flag = True\n",
    "                continue\n",
    "            elif arr[i]-arr[i-1] <= 0 and flag is None:\n",
    "                return False\n",
    "            elif arr[i] - arr[i-1] >= 0 and not flag:\n",
    "                return False\n",
    "            elif arr[i] - arr[i-1] < 0 and flag:\n",
    "                flag  = False\n",
    "            elif arr[i]-arr[i-1] <= 0 and flag:\n",
    "                return False\n",
    "        return not flag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        cur = 0\n",
    "        l = len(arr) - 1\n",
    "        if l < 2 or arr[0] >= arr[1]:\n",
    "            return False\n",
    "\n",
    "        for i in range(l):\n",
    "            if arr[i] == arr[i + 1]:\n",
    "                return False\n",
    "            if arr[i] > arr[i + 1]:\n",
    "                i += 1\n",
    "                break\n",
    "        \n",
    "        for i in range(i, l):\n",
    "            if arr[i] <= arr[i + 1]:\n",
    "                return False\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        n, i = len(arr), 0\n",
    "        while i + 1 < n and arr[i] < arr[i + 1]:\n",
    "            i += 1\n",
    "        if i == 0 or i == n - 1:\n",
    "            return False\n",
    "        while i + 1 < n and arr[i] > arr[i + 1]:\n",
    "            i += 1\n",
    "        return i == n - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        if len(arr) < 3:\n",
    "            return False\n",
    "        \n",
    "        l = 0\n",
    "        r = len(arr)-1\n",
    "        \n",
    "        while r > 0 and arr[r-1] > arr[r]:\n",
    "            r -= 1\n",
    "        while l < len(arr)-1 and arr[l+1] > arr[l]:\n",
    "            l += 1\n",
    "        if l != r or l == 0 or r == len(arr)-1:\n",
    "            return False\n",
    "        return True "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def validMountainArray(self, arr: List[int]) -> bool:\n",
    "\n",
    "#         size = len(arr)\n",
    "#         if size < 3:\n",
    "#             return False\n",
    "\n",
    "#         left, right = 0, size - 1\n",
    "\n",
    "#         while left <= right:\n",
    "#             if arr[left] == arr[left + 1] or arr[right] == arr[right - 1]:\n",
    "#                 return False\n",
    "#             if arr[left] < arr[left + 1]:\n",
    "#                 left += 1\n",
    "#             if arr[right] < arr[right - 1]:\n",
    "#                 right -= 1\n",
    "#             if arr[left] > arr[left + 1] and arr[right] > arr[right - 1]:\n",
    "#                 if left != right:\n",
    "#                     return False\n",
    "#                 else:\n",
    "#                     return True\n",
    "#             if left <= 0 or left >= size - 1 or right <=0 or right >= size - 1:\n",
    "#                 return False\n",
    "            \n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        N = len(arr)\n",
    "        i = 0\n",
    "\n",
    "        # 递增扫描\n",
    "        while i + 1 < N and arr[i] < arr[i + 1]:\n",
    "            i += 1\n",
    "\n",
    "        # 最高点不能是数组的第一个位置或最后一个位置\n",
    "        if i == 0 or i == N - 1:\n",
    "            return False\n",
    "\n",
    "        # 递减扫描\n",
    "        while i + 1 < N and arr[i] > arr[i + 1]:\n",
    "            i += 1\n",
    "\n",
    "        return i == N - 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        if len(arr) < 3:\n",
    "            return False\n",
    "        count = 0\n",
    "        while count < len(arr)-1 and arr[count]< arr[count+1]:\n",
    "             count+=1\n",
    "        if count == 0 or count == len(arr) -1:\n",
    "            return False\n",
    "        \n",
    "        while count < len(arr)-1 and arr[count] > arr[count+1]:\n",
    "            count+=1\n",
    "        # if count==len(arr)-1:\n",
    "        #     return True\n",
    "        return count==len(arr)-1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        # 双指针，一个从左边找最高山峰，一个从右边找最高山峰，最后判断找到的是不是同一个山峰\n",
    "        n = len(arr)\n",
    "        l,r = 0,n-1 # 定义两个指针的位置\n",
    "        # 从左边往右边找，一直找到山峰为止\n",
    "        while l < n - 1 and arr[l+1] > arr[l]:\n",
    "            l += 1\n",
    "        # 从右边往左边找，一直找到山峰为止\n",
    "        while r > 0 and arr[r] < arr[r-1]:\n",
    "            r -= 1\n",
    "        # 判断从左边和从右边找的山峰是不是同一个\n",
    "        return arr[l] == arr[r] and r == l and l < n-1 and r > 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        if len(arr) <= 2: return False\n",
    "        biggest = max(arr)\n",
    "        for i in range(len(arr)):\n",
    "            if arr[i] == biggest: break\n",
    "        if i == len(arr)-1: return False\n",
    "        if i == 0: return False\n",
    "        left = arr[:i+1]\n",
    "        right = arr[i:]\n",
    "        return all(left[j] < left[j+1] for j in range(len(left)-1)) and all(right[k] > right[k+1]for k in range(len(right)-1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "\n",
    "        size = len(arr)\n",
    "        if size < 3:\n",
    "            return False\n",
    "\n",
    "        left, right = 0, size - 1\n",
    "\n",
    "        while left <= right:\n",
    "            if arr[left] == arr[left + 1] or arr[right] == arr[right - 1]:\n",
    "                return False\n",
    "            if arr[left] < arr[left + 1]:\n",
    "                left += 1\n",
    "            if arr[right] < arr[right - 1]:\n",
    "                right -= 1\n",
    "            if arr[left] > arr[left + 1] and arr[right] > arr[right - 1]:\n",
    "                if left != right:\n",
    "                    return False\n",
    "                else:\n",
    "                    return True\n",
    "            if left <= 0 or left >= size - 1 or right <=0 or right >= size - 1:\n",
    "                return False\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        if len(arr) < 3 or arr[0] >= arr[1] or arr[-1] >= arr[-2]:\n",
    "            return False\n",
    "        flag = False\n",
    "        for i in range(1, len(arr) - 1):\n",
    "            if arr[i] >= arr[i + 1]:\n",
    "                flag = True\n",
    "            if arr[i] <= arr[i + 1] and flag:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        if len(arr) <= 1:\n",
    "            return False\n",
    "\n",
    "        left = 0\n",
    "        right = len(arr) - 1\n",
    "\n",
    "        \n",
    "        while arr[left] < arr[left + 1] and left < len(arr) - 2:\n",
    "            left += 1\n",
    "\n",
    "        while arr[right] < arr[right - 1] and right > 0:\n",
    "            right -= 1\n",
    "\n",
    "        return left > 0 and right < len(arr) - 1 and left == right\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        N = len(arr)\n",
    "        i = 0\n",
    "\n",
    "        # 递增扫描\n",
    "        while i + 1 < N and arr[i] < arr[i + 1]:\n",
    "            i += 1\n",
    "\n",
    "        # 最高点不能是数组的第一个位置或最后一个位置\n",
    "        if i == 0 or i == N - 1:\n",
    "            return False\n",
    "\n",
    "        # 递减扫描\n",
    "        while i + 1 < N and arr[i] > arr[i + 1]:\n",
    "            i += 1\n",
    "\n",
    "        return i == N - 1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr) -> bool:\n",
    "        add=1\n",
    "        reversed_time=0\n",
    "        for i in range(1,len(arr)):\n",
    "            # print(arr[i],arr[i-1],add,reversed_time)\n",
    "            if arr[i]*add>arr[i-1]*add:\n",
    "                if reversed_time==0:\n",
    "                    reversed_time+=1\n",
    "            elif arr[i]*add==arr[i-1]*add:\n",
    "                return False\n",
    "            else:\n",
    "                if reversed_time==1:\n",
    "                    add=-add\n",
    "                    reversed_time+=1\n",
    "                else:\n",
    "                    return False\n",
    "                \n",
    "        if reversed_time!=2:\n",
    "            return False\n",
    "        \n",
    "        return True\n",
    "            \n",
    "            \n",
    "x=Solution()\n",
    "print(x.validMountainArray([4,3,2,1]))\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        if len(arr)<3:\n",
    "            return False\n",
    "        if len(arr)==3: \n",
    "            if arr[1]>arr[0] and arr[1]>arr[2]:\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "        ma=arr[0]\n",
    "        k=0\n",
    "        for i in range(len(arr)):\n",
    "            if arr[i]>ma:\n",
    "                ma=arr[i]\n",
    "                k=i\n",
    "\n",
    "        if k==0 or k==len(arr)-1:\n",
    "            return False\n",
    "        for i in range(1,k):\n",
    "            if arr[i]<=arr[i-1]:\n",
    "\n",
    "                return False\n",
    "        for i in range(k+2,len(arr)):\n",
    "            if arr[i]>=arr[i-1]:\n",
    "\n",
    "                return False\n",
    "        return True\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        if len(arr) < 3:\n",
    "            return False\n",
    "        left, right = 0, 1\n",
    "        left_true, right_true = 0, 0\n",
    "        while right < len(arr)-1 and arr[right] > arr[left]:\n",
    "            right += 1\n",
    "            left += 1\n",
    "            left_true = 1\n",
    "        while right < len(arr) and arr[left] > arr[right]:\n",
    "            right += 1\n",
    "            left += 1\n",
    "            right_true = 1\n",
    "        if left == len(arr) - 1 and right_true and left_true:\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\r\n",
    "        \r\n",
    "        if len(arr) < 3:\r\n",
    "            return False\r\n",
    "        else:\r\n",
    "            top = max(arr)\r\n",
    "            top_index = arr.index(top)\r\n",
    "            if arr.count(top) != 1 or top_index == len(arr)-1 or top_index == 0:\r\n",
    "                return False\r\n",
    "            else:\r\n",
    "                return (all(arr[i]>arr[i-1] for i in range(1,top_index+1)) and all(arr[j]>arr[j+1] for j in range(top_index,len(arr)-1)))    \r\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        a = arr.index(max(arr))\n",
    "        if len(arr) == 2:\n",
    "            return False\n",
    "        b = arr[0:a+1]\n",
    "        c = arr[a::]\n",
    "        if b == sorted(b) and c == sorted(c, reverse=True):\n",
    "            if len(b) == 1 or len(c) == 1:\n",
    "                return False\n",
    "            for i in range(len(b)-1):\n",
    "                if b[i] == b[i+1]:\n",
    "                    return False\n",
    "            for j in range(len(c)-1):\n",
    "                if c[j] == c[j+1]:\n",
    "                    return False\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        ans = True\n",
    "        direct = True\n",
    "        stack = []\n",
    "        for x in arr:\n",
    "            if stack:\n",
    "                if stack[-1] == x:\n",
    "                    ans = False\n",
    "                    break\n",
    "                elif direct == 1:\n",
    "                    if len(stack) == 0:\n",
    "                        stack.append(x)\n",
    "                    elif len(stack) == 1 and stack[-1] > x:\n",
    "                        ans = False\n",
    "                        break\n",
    "                    elif len(stack) > 1 and stack[-1] > x:\n",
    "                        direct = -1\n",
    "                        stack.append(x)\n",
    "                    else:\n",
    "                        stack.append(x)\n",
    "                elif direct == -1 and stack[-1] < x:\n",
    "                    ans = False\n",
    "                    break\n",
    "            stack.append(x)\n",
    "        if direct == 1:\n",
    "            ans = False\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        A = arr\n",
    "        l,r=0,len(A)-1\n",
    "        while l<r and A[l]<A[l+1]: l+=1\n",
    "        while r>l and A[r]<A[r-1]: r-=1\n",
    "        return l==r and l!=0 and r!=len(A)-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        # x = True\n",
    "        # i = 0\n",
    "        # if len(arr) == 1:\n",
    "        #     x = False\n",
    "        # while i < len(arr) - 2:\n",
    "        #     if arr[i] == arr[i+1]:\n",
    "        #         x = False\n",
    "        #         break\n",
    "        #     elif arr[i] < arr[i+1]:\n",
    "        #         i += 1\n",
    "        #     else:\n",
    "        #         break\n",
    "        # while i < len(arr) - 1:\n",
    "        #     if arr[i] > arr[i+1] and i != 0:\n",
    "        #         i += 1\n",
    "        #     else:\n",
    "        #         x = False\n",
    "        #         break \n",
    "        # return x\n",
    "        x = True\n",
    "        i = 0\n",
    "        flag = 1\n",
    "        if len(arr) == 1 or arr[0] >= arr[1]:\n",
    "            x = False\n",
    "        while i < len(arr)-1:\n",
    "            if arr[i] == arr[i+1]:\n",
    "                x = False\n",
    "                break\n",
    "            if arr[i] > arr[i+1]:\n",
    "                flag = 0\n",
    "            if flag == 0 and arr[i] < arr[i+1]:\n",
    "                x = False\n",
    "                break\n",
    "            i += 1\n",
    "        if flag == 1:\n",
    "            x = False\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        flag = 1\n",
    "        if len(arr) < 3:\n",
    "            return False\n",
    "        for i in range(1, len(arr)):\n",
    "            if flag == 1:\n",
    "                if arr[i] > arr[i-1]:\n",
    "                    flag = 2\n",
    "                else:\n",
    "                    return False\n",
    "            elif flag == 2:\n",
    "                if arr[i] > arr[i-1]:\n",
    "                    pass\n",
    "                elif arr[i] < arr[i-1]:\n",
    "                    flag = 3\n",
    "                else:\n",
    "                    return False\n",
    "            else:\n",
    "                if arr[i] >= arr[i-1]:\n",
    "                    return False\n",
    "        if flag == 3:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\r\n",
    "        i,n = 0,len(arr)\r\n",
    "        if n < 3:\r\n",
    "            return False\r\n",
    "        while i < n-1:\r\n",
    "            if arr[i] < arr[i+1]:\r\n",
    "                i += 1\r\n",
    "            else:\r\n",
    "                break \r\n",
    "        if i == 0 or i == n-1:\r\n",
    "            return False\r\n",
    "        while i < n-1:\r\n",
    "            if arr[i] > arr[i+1]:\r\n",
    "                i += 1\r\n",
    "            else:\r\n",
    "                break \r\n",
    "        if i == n-1:\r\n",
    "            return True \r\n",
    "        else:\r\n",
    "            return False\r\n",
    "                \r\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        n = len(arr)\n",
    "        if n < 3:\n",
    "            return False\n",
    "        peak = arr.index(max(arr))\n",
    "        if peak == 0 or peak == n-1:\n",
    "            return False\n",
    "        for i in range(n):\n",
    "            if i < peak:\n",
    "                if arr[i] >= arr[i+1]:\n",
    "                    return False\n",
    "            elif i > peak:\n",
    "                if arr[i] >= arr[i-1]:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        # 双指针，一个从左边找最高山峰，一个从右边找最高山峰，最后判断找到的是不是同一个山峰\n",
    "        n = len(arr)\n",
    "        l,r = 0,n-1 # 定义两个指针的位置\n",
    "        # 从左边往右边找，一直找到山峰为止\n",
    "        while l < n - 1 and arr[l+1] > arr[l]:\n",
    "            l += 1\n",
    "        # 从右边往左边找，一直找到山峰为止\n",
    "        while r > 0 and arr[r] < arr[r-1]:\n",
    "            r -= 1\n",
    "        # 判断从左边和从右边找的山峰是不是同一个\n",
    "        return arr[l] == arr[r] and r == l and l < n-1 and r > 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        if len(arr)<3:\n",
    "            return False\n",
    "\n",
    "        max_index = arr.index(max(arr))\n",
    "\n",
    "        if max_index == len(arr)-1 or max_index==0:\n",
    "            return False\n",
    "\n",
    "        i=0\n",
    "\n",
    "        while i<len(arr):\n",
    "            if i < max_index:\n",
    "                if arr[i]>=arr[i+1]:\n",
    "                    return False\n",
    "\n",
    "            if i > max_index:\n",
    "                if arr[i]>=arr[i-1]:\n",
    "                    return False\n",
    "            \n",
    "            i+=1\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, nums: List[int]) -> bool:\n",
    "        n = len(nums)\n",
    "        if n < 3:\n",
    "            return False\n",
    "        i = 0\n",
    "        while i + 1 < n and nums[i] < nums[i + 1]:\n",
    "            i += 1\n",
    "        \n",
    "        if i == 0 or i == n-1:\n",
    "            return False\n",
    "\n",
    "        while i + 1 < n and nums[i] > nums[i + 1]:\n",
    "            i += 1\n",
    "        \n",
    "        return i == n-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: list[int]) -> bool:\n",
    "        idx_max = arr.index(max(arr))\n",
    "        if idx_max == 0 or idx_max == len(arr) - 1:\n",
    "            return False\n",
    "        arr_1 = arr[:idx_max + 1]\n",
    "        arr_2 = arr[idx_max + 1:]\n",
    "        arr_1.sort()\n",
    "        arr_2.sort(reverse = True)\n",
    "        for i in range(1, len(arr)):\n",
    "            if arr[i] == arr[i - 1]:\n",
    "                return False\n",
    "        if arr_1 == arr[:idx_max + 1] and arr_2 == arr[idx_max + 1:]:\n",
    "            return True\n",
    "\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def validMountainArray(self, arr: List[int]) -> bool:\n",
    "        if len(arr) < 3:\n",
    "            return False\n",
    "        left, right = 0, len(arr)-1\n",
    "        while left < len(arr)-1:\n",
    "            if arr[left] < arr[left+1]:\n",
    "                left += 1\n",
    "            else:\n",
    "                break\n",
    "        while right > 0:\n",
    "            if arr[right] < arr[right-1]:\n",
    "                right -= 1\n",
    "            else:\n",
    "                break\n",
    "        if left == right and left != 0 and right != len(arr)-1:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
